import AttributeUtil from '@/AttributeUtil';
import BaseUtil from '@/BaseUtil';
import ItemUtil from '@/ItemUtil';
import UnitItemEvent from '@/event/UnitItemEvent';

declare global {
    interface RecipeConfigModel {
        [index: string]: number;
    }
}
export default class RecipeSystem {
    recipeList: Map<string, RecipeConfigModel> = new Map();
    constructor() {
        se.onUnitPickupItem(RecipeSystem.takeEquipment);
    }
    static takeEquipment(this: void, d: UnitItemEvent) {
        let u = GetTriggerUnit();
        BaseUtil.runLater(0.1, () => {
            for (let [key, value] of RecipeSystemInstance.recipeList) {
                let can = true;
                for (let id in value) {
                    let c = value[id];
                    let tc = ItemUtil.getItemCountOfTypeFromUnit(u, id);
                    if (tc < c) {
                        can = false;
                    }
                }
                if (can) {
                    let attr = {};
                    for (let id in value) {
                        for (let i = 0; i < value[id]; i++) {
                            let it = ItemUtil.getItemOfTypeFromUnit(u, id);
                            let itemAttr = AttributeUtil.getItemAttribute(it) ?? {};
                            AttributeUtil.add(attr, itemAttr);
                            RemoveItem(it);
                        }
                    }
                    let it = CreateItem(key, GetUnitX(u), GetUnitY(u));
                    AttributeUtil.setItemAttribute(it, attr);
                    UnitAddItem(u, it);
                }
            }
        });
    }
    addRecipe(结果: string, 配置: RecipeConfigModel) {
        if (RecipeSystemInstance.recipeList.has(结果)) {
            return;
        }
        RecipeSystemInstance.recipeList.set(结果, 配置);
    }
}
